﻿using System.Web.Mvc;
using Beeldbankmvc.Models;

namespace Beeldbankmvc.Controllers
{
    public class CityController : Controller
    {
        CityRepository CityRepository = new CityRepository();

        //
        // GET: /City/

        public ActionResult Index()
        {
            var cityModels = CityRepository.getCityAll();

            return View(cityModels);
        }

        //
        // GET: /City/Details/5

        public ActionResult Details(int id)
        {
            var cityModel = CityRepository.FindOne(id);
            return View(cityModel);
        }

        //
        // GET: /City/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /City/Create

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                CityModel CityModel = new CityModel();

                // update the model with all the form fields contained in the FormCollection
                UpdateModel(CityModel);

                CityRepository.Create(CityModel);
                CityRepository.Save();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /City/Edit/5
 
        public ActionResult Edit(int id)
        {
            var cityModel = CityRepository.FindOne(id);
            return View(cityModel);
        }

        //
        // POST: /City/Edit/5

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                CityModel CityModel = new CityModel();

                // update the model with all the form fields contained in the FormCollection
                UpdateModel(CityModel);

                CityRepository.Update(CityModel);
                CityRepository.Save();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /City/Delete/5
 
        public ActionResult Delete(int id)
        {
            var cityModel = CityRepository.FindOne(id);
            return View(cityModel);
        }

        //
        // POST: /City/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                CityRepository.Delete(id);
                CityRepository.Save();
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
